package com.appkubes.mall.restful.application.user.address.service.impl;

import com.appkubes.mall.core.entity.MallUserAddress;
import com.appkubes.mall.core.service.impl.BaseMallUserAddressServiceImpl;
import com.appkubes.mall.restful.application.user.address.entity.vo.MallUserAddressVo;
import com.appkubes.mall.restful.application.user.address.service.UserAddressService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl extends BaseMallUserAddressServiceImpl implements UserAddressService {

    @Override
    public List<MallUserAddress> querySelective(String userKey, Integer isDefault) {
        MallUserAddress query = new MallUserAddress();
        query.setUserKey(userKey);
        query.setIsDefault(isDefault);

        List<MallUserAddress> list = this.selectListByEntity(query);
        return list;
    }

    @Override
    public Boolean setDefault(MallUserAddressVo mallUserAddressVo) {
        return setDefault(mallUserAddressVo.getUserKey(), mallUserAddressVo.getAddressKey());
    }

    public Boolean setDefault(String userKey, String addressKey) {
        List<MallUserAddress> list = this.querySelective(userKey, null);
        for (MallUserAddress model : list) {
            model.setIsDefault(0);
            this.updateById(model);
        }
        MallUserAddress updateModel = this.getById(addressKey);
        updateModel.setIsDefault(1);
        return this.updateById(updateModel);
    }

    public Boolean setDefaultByUid(String addressKey) {
        MallUserAddress updateModel = this.getById(addressKey);
        updateModel.setIsDefault(1);
        return this.updateById(updateModel);
    }

    @Override
    public MallUserAddress getDefault(MallUserAddressVo mallUserAddressVo) {
        MallUserAddress query = new MallUserAddress();
        query.setUserKey(mallUserAddressVo.getUserKey());
        query.setIsDefault(1);

        List<MallUserAddress> mallUserAddressList = this.selectListByEntity(query);
        if (mallUserAddressList.size() == 0) return null;
        return mallUserAddressList.get(0);
    }


}
